package saxion.security.android;

/*
 * Copyright (C) 2007 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


import java.io.File;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Environment;
import android.preference.EditTextPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;


/**
 * Settings activity
 * @author Saxion Security
 *
 */
public class SettingsActivity extends PreferenceActivity {
	
	private String defaultFolder;
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        
        //loads the preferences from a resource
        addPreferencesFromResource(R.layout.preferences);
        
        //CheckBoxPreference installCheckBox = (CheckBoxPreference) findPreference("install");
        //preferences.removePreference(installCheckBox);
        
        //gets the right strings from strings.xml
        defaultFolder = getString(R.string.default_encrypted_folder);
        

        
        getPreferenceManager().findPreference("reset").setOnPreferenceClickListener(new OnPreferenceClickListener() {

        	/**
        	 * The action when an user clicks on the preference "reset"
        	 */
            public boolean onPreferenceClick(Preference preference) {
				
            	
                //String newFolder = "/encryptedfiles";
                String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
                String folderLocation = extStorageDirectory + defaultFolder;
                
                
				//only create id if it the dir doesnt exsists
				File dir = new File(folderLocation);
				if(dir.exists() == false) {
				    // if the dir does not exsist
				    dir.mkdir();
				}
				
				PreferenceManager pm = preference.getPreferenceManager();
				SharedPreferences prefs = pm.getSharedPreferences();
				
				// edit the preferences
				SharedPreferences.Editor editor = prefs.edit();
				editor.putString("encryptedFolderLocation", folderLocation);
				editor.commit(); // Very important
				
				EditTextPreference et = (EditTextPreference) pm.findPreference("encryptedFolderLocation");
				et.setText(folderLocation);
				
				return false;

            }
        });

    }

}
